<template>
  <div style="padding: 10px">
    <!--    功能区-->
    <div style="margin: 10px 0px">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <!--    搜索区-->
    <div style="margin: 10px 0px">
      <el-input v-model="search" placeholder="请输入姓名" style="width: 10%" clearable></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
    </div>

    <el-table :data="tableData" border stripe style="width: 100%" >
      <el-table-column prop="id" label="id"  sortable />
      <el-table-column label="角色">
        <template #default="scope">
          <span v-if="scope.row.rid === 1"> 管理员</span>
          <span v-if="scope.row.rid === 2"> 区域管理员</span>
          <span v-if="scope.row.rid === 3"> 用户</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名"  />
      <el-table-column prop="username" label="用户名"  />
      <el-table-column prop="phone" label="手机号" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column label="操作" width="350">
        <template #default="scope">
          <el-button type="primary"  @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger" >删除</el-button>
            </template>
          </el-popconfirm>
        </template>

      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          :currentPage="currentPage"
          :page-size="pageSize"
          :page-sizes="[5, 10, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog
        v-model="dialogVisible" :title="title" width="30%">
      <el-form :model="form" label-width="120px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" style="width: 55%" />
        </el-form-item>
        <el-form-item label="角色">
          <el-input v-model="form.rid" style="width: 55%" />
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.username" style="width: 55%" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone" style="width: 55%" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" style="width: 55%" />
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>

import request from "../utils/request";

export default {
  name: 'Admin',
  components: {
  },
  data() {
    return{
      search:"",
      total:10,
      currentPage:1,
      pageSize:10,
      dialogVisible:false,
      form:{},
      tableData:[],
    }
  },

  methods:{
    load(){
      request.get("/user/admin",{
        params:{
          currentPage : this.currentPage,
          pageSize: this.pageSize,
          search:this.search
        }
      }).then(res =>{
          this.tableData = res.data.data
          this.total = res.data.data.length
      })
    },
    add(){
      this.dialogVisible = true;
      this.title = "新增管理员"
      this.form = {}
    },
    submit(){
      if (this.form.id) {
        // 修改
        request.put("/user", this.form).then(res => {
          console.log(res)
          if (res.data.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功！"
            })
            this.dialogVisible = false;
            this.load();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg
            })
          }
        })
      }else {
        // 新增
        request.post("/user",this.form).then(res =>{
          // this.tableData = res.data
          console.log(res)
          if (res.data.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功！"
            })
            this.dialogVisible = false;
            this.load();
          } else {
            this.$message({
              type: "error",
              message: res.data.msg
            })
          }
        })
      }
    },
    handleEdit(row){
      // 为避免取消后数据改变
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
      this.title = "编辑用户"
    },
    handleDelete(id){
      console.log(id)
      request.delete("/user/" + id).then(res =>{
        if (res.data.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功！"
          })
          this.load();
        } else {
          this.$message({
            type: "error",
            message: res.data.msg
          })
        }
      })
    },
    // 改变当前每页的个数触发
    handleSizeChange(pageSize){
      this.pageSize = pageSize
      this.load()
    },
    // 改变当前页码触发
    handleCurrentChange(pageNum){
      this.currentPage = pageNum
      this.load()
    },
  },
  created() {
    this.load()
  }
}
</script>
